<?php
//
//	file: includes/aucp/handler_pm.php
//	begin: 2006-07-10
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: this is the main class for pms

/*
 Notes and such:
The database schema will be changed slightly:
First, there are a few new fields to phpbb_privmsgs:
	privmsgs_text_id, which links to phpbb_privmsgs_text.privmsgs_text_id
	privmsgs_users_to, which is the complete list of recipients
	privmsgs_users_cc, which is the complete list of cc recipients
Secondly, each recipient will have a separate row in phpbb_privmsgs, differentiated by the original privmsgs_to_user
Thirdly, the sender will have one message in his out/sent box, regardless of the number of recipients (thanks to a minor change in the sql statement)
Fourth, once *any* recipient reads the message, the sender's copy will be moved to his sentbox
	and will be unable to edit it further
From now on, the phpbb_privmsgs_text table will have one entry for all recipients (to save space)
BCC recipients (if it is enabled) will only be listed in privmsgs_to_user, not in either privmsgs_users_to or
	privmsgs_users_cc
*/

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class aucp_pm
{
	// string/integer vars
	var $subject, $subtitle, $message;
	// array vars
	var $users, $notify_type;

	// @constructor initialize
	function __constructor()
	{
		global $config;

		// plugs, but we're also pulling all the pm plugins from vanilla CH
		$plug_ins = new plug_ins();
		$plug_ins->load('aucp_pm');
		unset($plug_ins);
		$config->plug_ins['aucp_pm'] = array_merge($config->plug_ins['aucp_pm'], $config->plug_ins['privmsg']);
		$this->plug_ins = &$config->plug_ins['aucp_pm'];
	}

	// @backward compatibility
	function aucp_pm()
	{
		return $this->__constructor();
	}

	// @public empties fields
	function reset($notify_type='pm')
	{
		global $config, $user, $notify_types;

		$this->users = $this->vars = array();
		$this->subject = $this->subtitle = $this->template = $this->tpl_file = '';

		$this->notify_type = 'user_notify_' . $notify_type;

		return;
	}

	// @public adds user to addressed users
	function add_user($user_to, $type='to')
	{
		global $config;

		// send only to users who want the notification
		if ( ($user_to['user_active'] || $config->data['advnot_send_inactive']) && $user_to['user_notify'] && $user_to[$this->notify_type])
		{
			// group according to recipient type
			$this->users[$type][] = $user_to['user_id'];
		}
	}

	// @public prepare the subject
	function set_subject($subject='', $subtitle='')
	{
		global $user;

		if (!empty($subject))
		{
			$this->subject = trim(htmlspecialchars($subject));

			if (!empty($subtitle))
			{
				$this->subtitle = trim(htmlspecialchars($subtitle));
			}
		}
	}

	// @public prepares a message body
	function set_message($message='')
	{
		global $config, $user;

		// get misc data for preparing
		$html_on = ($config->data['allow_html']) ? !_read('disable_html', TYPE_INT, !$user->data['user_allowhtml']) : 0;
		$bbcode_on = ($config->data['allow_bbcode']) ? !_read('disable_bbcode', TYPE_INT, !$user->data['user_allowbbcode']) : 0;
		$smilies_on = ($config->data['allow_smilies']) ? !_read('disable_smilies', TYPE_INT, !$user->data['user_allowsmile']) : 0;
		$attach_sig = ($config->data['allow_sig']) ? !_read('attach_sig', TYPE_INT, !$user->data['user_attachsig']) : 0;
		$bbcode_uid = ($this->data['bbcode_on']) ? make_bbcode_uid() : 0;

		$this->message = prepare_message($message, $html_on, $bbcode_on, $smilies_on, $bbcode_uid);

		return;
	}

	// @public send post
	function send ()
	{
		global $db, $config, $user, $notify;
		global $error;

		// check for errors
		if (empty($this->users))
		{
			_error('pm_no_recipients');
		}
		if (empty($this->subject))
		{
			_error('pm_no_subject');
		}
		if (empty($this->message))
		{
			_error('pm_no_message');
		}

		// let whatever called this deal with the error message!
		if ( $error )
		{
			return false;
		}

		$users_to = implode(', ', $this->users['to']);
		$users_cc = implode(', ', $this->users['cc']);
		$all_users = array_merge($users_to, $users_cc, $this->users['bcc']);

		// flood control
		$sql = "SELECT MAX(privmsgs_date) AS last_post_time
			FROM " . PRIVMSGS_TABLE . "
			WHERE privmsgs_from_userid = " . $user->data['user_id'];
		if ( $result = $db->sql_query($sql, false, __LINE__, __FILE__) )
		{
			$db_row = $db->sql_fetchrow($result);

			$last_post_time = $db_row['last_post_time'];
			$current_time = time();

			if ( ( $current_time - $last_post_time ) < $config->data['flood_interval'])
			{
				message_die(GENERAL_MESSAGE, $user->lang('Flood_Error'));
			}
		}

		$sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';

		// this way, when one recipient reads a message (or moves it), it won't affect all other recipients
		$sql = "SELECT MAX(privmsgs_text_id) AS text_id
			FROM " . PRIVMSGS_TEXT_TABLE;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$db_row = $db->sql_fetchrow($result);
		$privmsg_sent_id = $db_row['text_id'];

		$sql_info_front = 'INSERT INTO ' . PRIVMSGS_TABLE . ' (privmsgs_type, privmsgs_text_id, privmsgs_box, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_to_users, privmsgs_cc_users, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig)
				VALUES (' . PRIVMSGS_NEW_MAIL . ',' . $privmsg_sent_id . ', ' . PM_BOX_IN . ', \'' . str_replace("\'", "''", $this->subject) . '\', ' . $user->data['user_id'] . ', \'';
		$sql_info_end = '\', \'' . $users_to . '\', \'' . $users_cc . '\', ' . time() . ', ' . $user->session->ip .', ' . $this->data['html_on'] . ', ' . $this->data['bbcode_on'] . ', ' . $this->data['smilies_on'] . ', ' . $this->data['attach_sig'] . ')';
		foreach ($all_users as $to_user)
		{
			$sql_info = $sql_info_front . $to_user . $sql_info_end;
			$db->sql_query($sql_info, BEGIN_TRANSACTION, __LINE__, __FILE__);
		}
		$sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
			VALUES ($privmsg_sent_id, '" . $this->data['bbcode_uid'] . "', '" . str_replace("\'", "''", $this->message) . "')";
		$db->sql_query($sql, END_TRANSACTION, __LINE__, __FILE__);

		// add to the users new pm counter
		$sql = 'UPDATE ' . USERS_TABLE . '
			SET user_new_privmsg = user_new_privmsg + 1, user_last_privmsg = ' . time() . '
			WHERE user_id IN ( ' . $all_users . ' )';
		$db->sql_query($sql, false, __LINE__, __FILE__);
		$notify->pm_to_email();
		return;
	}

	// @public dead function
	function add_headers($dummy=false)
	{
		return;
	}

	// @public dead function
	function set_tpl($dummy=false)
	{
		return;
	}

	// @public dead function
	function assign_vars($dummy=false)
	{
		return;
	}

	// @public displays a pm
	function display()
	{
		global $config, $user, $template;

		$privmsgs_id = _read(POST_POST_URL, TYPE_INT, 0);

		if (!$privmsgs_id)
		{
			message_die(GENERAL_ERROR, $user->lang('No_post_id'));
		}

		if ( !$userdata['session_logged_in'] )
		{
			redirect($config->url('login', array('redirect' => 'privmsg', 'folder' => $folder, 'mode' => $mode, POST_POST_URL => $privmsg_id), true);
		}

		// determine the type of box

		// main sql statement
	switch( $folder )
	{
		case 'in':
			$pm_sql_user = "AND pm.privmsgs_to_userid = " . $userdata['user_id'] . "
				AND ( pm.privmsgs_type = " . PRIVMSGS_READ_MAIL . "
					OR pm.privmsgs_type = " . PRIVMSGS_NEW_MAIL . "
					OR pm.privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
			break;
		case 'out':
			$pm_sql_user = "AND pm.privmsgs_from_userid =  " . $userdata['user_id'] . "
				AND ( pm.privmsgs_type = " . PRIVMSGS_NEW_MAIL . "
					OR pm.privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) ";
			break;
		case 'mix':
			$pm_sql_user = "AND ( ( pm.privmsgs_to_userid = " . $userdata['user_id'] . "
					AND pm.privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " )
				OR ( pm.privmsgs_from_userid = " . $userdata['user_id'] . "
					AND pm.privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " )
				)";
			break;
	}

	//
	// Major query obtains the message ...
	//
	$sql = "SELECT u.username AS username_1, u.user_id AS user_id_1, u2.username AS username_2, u2.user_id AS user_id_2, u.user_sig_bbcode_uid, u.user_posts, u.user_from, u.user_website, u.user_email, u.user_icq, u.user_aim, u.user_yim, u.user_regdate, u.user_msnm, u.user_viewemail, u.user_rank, u.user_sig, u.user_avatar, pm.*, pmt.privmsgs_bbcode_uid, pmt.privmsgs_text
		FROM " . PRIVMSGS_TABLE . " pm, " . PRIVMSGS_TEXT_TABLE . " pmt, " . USERS_TABLE . " u, " . USERS_TABLE . " u2
		WHERE pm.privmsgs_id = $privmsgs_id
			AND pmt.privmsgs_text_id = pm.privmsgs_id
			$pm_sql_user
			AND u.user_id = pm.privmsgs_from_userid
			AND u2.user_id = pm.privmsgs_to_userid";
		if ( $this->plug_ins )
		{
			foreach ( $this->plug_ins as $plug => $dummy )
			{
				if ( method_exists($this->plug_ins[$plug], 'pre_process') )
				{
					$this->plug_ins[$plug]->pre_process($sql);
				}
			}
		}

		$result = $db->sql_query($sql, false, __LINE__, __FILE__);

	//
	// Did the query return any data?
	//
	if ( !($privmsg = $db->sql_fetchrow($result)) )
	{
		redirect(append_sid("privmsg.$phpEx?folder=$folder", true));
	}

	$privmsg_id = $privmsg['privmsgs_id'];

	// transfer a previously new message to sender's sentbox

	if (($privmsg['privmsgs_type'] == PRIVMSGS_NEW_MAIL || $privmsg['privmsgs_type'] == PRIVMSGS_UNREAD_MAIL) && $folder == 'inbox')
	{
		// Update appropriate counter
		switch ($privmsg['privmsgs_type'])
		{
			case PRIVMSGS_NEW_MAIL:
				$sql = "user_new_privmsg = user_new_privmsg - 1";
				break;
			case PRIVMSGS_UNREAD_MAIL:
				$sql = "user_unread_privmsg = user_unread_privmsg - 1";
				break;
		}

		$sql = "UPDATE " . USERS_TABLE . "
			SET $sql
			WHERE user_id = " . $userdata['user_id'];
		$db->sql_query($sql, false, __LINE__, __FILE__);

		$sql = "UPDATE " . PRIVMSGS_TABLE . "
			SET privmsgs_type = " . PRIVMSGS_READ_MAIL . "
			WHERE privmsgs_id = " . $privmsg['privmsgs_id'];
		$db->sql_query($sql, false, __LINE__, __FILE__);

		// Check to see if the poster has a 'full' sent box
		$sql = "SELECT COUNT(privmsgs_id) AS sent_items, MIN(privmsgs_date) AS oldest_post_time
			FROM " . PRIVMSGS_TABLE . "
			WHERE privmsgs_type = " . PRIVMSGS_SENT_MAIL . "
				AND privmsgs_from_userid = " . $privmsg['privmsgs_from_userid'];
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);

		$sql_priority = '';

		if ( $sent_info = $db->sql_fetchrow($result) )
		{
			if ($board_config['max_sentbox_privmsgs'] && $sent_info['sent_items'] >= $board_config['max_sentbox_privmsgs'])
			{
				$sql = "SELECT privmsgs_id FROM " . PRIVMSGS_TABLE . "
					WHERE privmsgs_type = " . PRIVMSGS_SENT_MAIL . "
						AND privmsgs_date = " . $sent_info['oldest_post_time'] . "
						AND privmsgs_from_userid = " . $privmsg['privmsgs_from_userid'];
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$old_privmsgs_id = $db->sql_fetchrow($result);
				$old_privmsgs_id = $old_privmsgs_id['privmsgs_id'];

				$sql = "DELETE $sql_priority FROM " . PRIVMSGS_TABLE . "
					WHERE privmsgs_id = $old_privmsgs_id";
				$db->sql_query($sql, false, __LINE__, __FILE__);

				$sql = "DELETE $sql_priority FROM " . PRIVMSGS_TEXT_TABLE . "
					WHERE privmsgs_text_id = $old_privmsgs_id";
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// plugs
				if ( $this->plug_ins )
				{
					foreach ( $this->plug_ins as $plug => $dummy )
					{
						if ( method_exists($this->plug_ins[$plug], 'delete') )
						{
							$this->plug_ins[$plug]->delete($old_privmsgs_id);
						}
					}
				}
			}
		}

		//
		// This makes a copy of the post and stores it as a SENT message from the sendee. Perhaps
		// not the most DB friendly way but a lot easier to manage, besides the admin will be able to
		// set limits on numbers of storable posts for users ... hopefully!
		//
		$sql = "INSERT $sql_priority INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig)
			VALUES (" . PRIVMSGS_SENT_MAIL . ", '" . str_replace("\'", "''", addslashes($privmsg['privmsgs_subject'])) . "', " . $privmsg['privmsgs_from_userid'] . ", " . $privmsg['privmsgs_to_userid'] . ", " . $privmsg['privmsgs_date'] . ", '" . $privmsg['privmsgs_ip'] . "', " . $privmsg['privmsgs_enable_html'] . ", " . $privmsg['privmsgs_enable_bbcode'] . ", " . $privmsg['privmsgs_enable_smilies'] . ", " .  $privmsg['privmsgs_attach_sig'] . ")";
		$db->sql_query($sql, false, __LINE__, __FILE__);

		$privmsg_sent_id = $db->sql_nextid();

		$sql = "INSERT $sql_priority INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
			VALUES ($privmsg_sent_id, '" . $privmsg['privmsgs_bbcode_uid'] . "', '" . str_replace("\'", "''", addslashes($privmsg['privmsgs_text'])) . "')";
		$db->sql_query($sql, false, __LINE__, __FILE__);

		// plugs
		if ( $this->plug_ins )
		{
			foreach ( $this->plug_ins as $plug => $dummy )
			{
				if ( method_exists($this->plug_ins[$plug], 'duplicate') )
				{
					$this->plug_ins[$plug]->duplicate($privmsg_sent_id);
				}
			}
		}
	}

	//
	// Pick a folder, any folder, so long as it's one below ...
	//
	$post_urls = array(
		'post' => append_sid("privmsg.$phpEx?mode=post"),
		'reply' => append_sid("privmsg.$phpEx?mode=reply&amp;" . POST_POST_URL . "=$privmsg_id"),
		'quote' => append_sid("privmsg.$phpEx?mode=quote&amp;" . POST_POST_URL . "=$privmsg_id"),
		'edit' => append_sid("privmsg.$phpEx?mode=edit&amp;" . POST_POST_URL . "=$privmsg_id")
	);
	$post_icons = array(
		'post_img' => '<a href="' . $post_urls['post'] . '"><img src="' . $images['pm_postmsg'] . '" alt="' . $lang['Post_new_pm'] . '" border="0" /></a>',
		'post' => '<a href="' . $post_urls['post'] . '">' . $lang['Post_new_pm'] . '</a>',
		'reply_img' => '<a href="' . $post_urls['reply'] . '"><img src="' . $images['pm_replymsg'] . '" alt="' . $lang['Post_reply_pm'] . '" border="0" /></a>',
		'reply' => '<a href="' . $post_urls['reply'] . '">' . $lang['Post_reply_pm'] . '</a>',
		'quote_img' => '<a href="' . $post_urls['quote'] . '"><img src="' . $images['pm_quotemsg'] . '" alt="' . $lang['Post_quote_pm'] . '" border="0" /></a>',
		'quote' => '<a href="' . $post_urls['quote'] . '">' . $lang['Post_quote_pm'] . '</a>',
		'edit_img' => '<a href="' . $post_urls['edit'] . '"><img src="' . $images['pm_editmsg'] . '" alt="' . $lang['Edit_pm'] . '" border="0" /></a>',
		'edit' => '<a href="' . $post_urls['edit'] . '">' . $lang['Edit_pm'] . '</a>'
	);

	if ( $folder == 'inbox' )
	{
		$post_img = $post_icons['post_img'];
		$reply_img = $post_icons['reply_img'];
		$quote_img = $post_icons['quote_img'];
		$edit_img = '';
		$post = $post_icons['post'];
		$reply = $post_icons['reply'];
		$quote = $post_icons['quote'];
		$edit = '';
		$l_box_name = $lang['Inbox'];
	}
	else if ( $folder == 'outbox' )
	{
		$post_img = $post_icons['post_img'];
		$reply_img = '';
		$quote_img = '';
		$edit_img = $post_icons['edit_img'];
		$post = $post_icons['post'];
		$reply = '';
		$quote = '';
		$edit = $post_icons['edit'];
		$l_box_name = $lang['Outbox'];
	}
	else if ( $folder == 'savebox' )
	{
		if ( $privmsg['privmsgs_type'] == PRIVMSGS_SAVED_IN_MAIL )
		{
			$post_img = $post_icons['post_img'];
			$reply_img = $post_icons['reply_img'];
			$quote_img = $post_icons['quote_img'];
			$edit_img = '';
			$post = $post_icons['post'];
			$reply = $post_icons['reply'];
			$quote = $post_icons['quote'];
			$edit = '';
		}
		else
		{
			$post_img = $post_icons['post_img'];
			$reply_img = '';
			$quote_img = '';
			$edit_img = '';
			$post = $post_icons['post'];
			$reply = '';
			$quote = '';
			$edit = '';
		}
		$l_box_name = $lang['Saved'];
	}

	$s_hidden_fields = '<input type="hidden" name="mark[]" value="' . $privmsgs_id . '" />';

	$page_title = $lang['Read_pm'];

	//
	// Load templates
	//
	$template->set_filenames(array(
		'body' => 'privmsgs_read_body.tpl')
	);

	$template->assign_vars(array(
		'INBOX_IMG' => $inbox_img,
		'SENTBOX_IMG' => $sentbox_img,
		'OUTBOX_IMG' => $outbox_img,
		'SAVEBOX_IMG' => $savebox_img,
		'INBOX' => $inbox_url,

		'POST_PM_IMG' => $post_img,
		'REPLY_PM_IMG' => $reply_img,
		'EDIT_PM_IMG' => $edit_img,
		'QUOTE_PM_IMG' => $quote_img,
		'POST_PM' => $post,
		'REPLY_PM' => $reply,
		'EDIT_PM' => $edit,
		'QUOTE_PM' => $quote,

		'SENTBOX' => $sentbox_url,
		'OUTBOX' => $outbox_url,
		'SAVEBOX' => $savebox_url,

		'BOX_NAME' => $l_box_name,

		'L_MESSAGE' => $lang['Message'],
		'L_INBOX' => $lang['Inbox'],
		'L_OUTBOX' => $lang['Outbox'],
		'L_FLAG' => $lang['Flag'],
		'L_SUBJECT' => $lang['Subject'],
		'L_POSTED' => $lang['Posted'],
		'L_DATE' => $lang['Date'],
		'L_FROM' => $lang['From'],
		'L_TO' => $lang['To'],
		'L_SAVE_MSG' => $lang['Save_message'],
		'L_DELETE_MSG' => $lang['Delete_message'],

		'S_PRIVMSGS_ACTION' => append_sid("privmsg.$phpEx?folder=$folder"),
		'S_HIDDEN_FIELDS' => $s_hidden_fields)
	);

	$username_from = $privmsg['username_1'];
	$user_id_from = $privmsg['user_id_1'];
	$username_to = $privmsg['username_2'];
	$user_id_to = $privmsg['user_id_2'];

	$post_date = create_date($board_config['default_dateformat'], $privmsg['privmsgs_date'], $board_config['board_timezone']);

	$temp_url = append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '=' . $user_id_from);
	$profile_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_profile'] . '" alt="' . $lang['Read_profile'] . '" title="' . $lang['Read_profile'] . '" border="0" /></a>';
	$profile = '<a href="' . $temp_url . '">' . $lang['Read_profile'] . '</a>';

	$temp_url = append_sid("privmsg.$phpEx?mode=post&amp;" . POST_USERS_URL . "=$user_id_from");
	$pm_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_pm'] . '" alt="' . $lang['Send_private_message'] . '" title="' . $lang['Send_private_message'] . '" border="0" /></a>';
	$pm = '<a href="' . $temp_url . '">' . $lang['Send_private_message'] . '</a>';

	if ( (!empty($privmsg['user_viewemail']) && empty($config->data['email_disable'])) || ($user->data['user_level'] == ADMIN) )
	{
		$email_uri = ( $board_config['board_email_form'] ) ? append_sid("profile.$phpEx?mode=email&amp;" . POST_USERS_URL .'=' . $user_id_from) : 'mailto:' . $privmsg['user_email'];

		$email_img = '<a href="' . $email_uri . '"><img src="' . $images['icon_email'] . '" alt="' . $lang['Send_email'] . '" title="' . $lang['Send_email'] . '" border="0" /></a>';
		$email = '<a href="' . $email_uri . '">' . $lang['Send_email'] . '</a>';
	}
	else
	{
		$email_img = '';
		$email = '';
	}

	$www_img = ( $privmsg['user_website'] ) ? '<a href="' . $privmsg['user_website'] . '" target="_userwww"><img src="' . $images['icon_www'] . '" alt="' . $lang['Visit_website'] . '" title="' . $lang['Visit_website'] . '" border="0" /></a>' : '';
	$www = ( $privmsg['user_website'] ) ? '<a href="' . $privmsg['user_website'] . '" target="_userwww">' . $lang['Visit_website'] . '</a>' : '';

	if ( !empty($privmsg['user_icq']) )
	{
		$icq_status_img = '<a href="http://wwp.icq.com/' . $privmsg['user_icq'] . '#pager"><img src="http://web.icq.com/whitepages/online?icq=' . $privmsg['user_icq'] . '&img=5" width="18" height="18" border="0" /></a>';
		$icq_img = '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $privmsg['user_icq'] . '"><img src="' . $images['icon_icq'] . '" alt="' . $lang['ICQ'] . '" title="' . $lang['ICQ'] . '" border="0" /></a>';
		$icq =  '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $privmsg['user_icq'] . '">' . $lang['ICQ'] . '</a>';
	}
	else
	{
		$icq_status_img = '';
		$icq_img = '';
		$icq = '';
	}

	$aim_img = ( $privmsg['user_aim'] ) ? '<a href="aim:goim?screenname=' . $privmsg['user_aim'] . '&amp;message=Hello+Are+you+there?"><img src="' . $images['icon_aim'] . '" alt="' . $lang['AIM'] . '" title="' . $lang['AIM'] . '" border="0" /></a>' : '';
	$aim = ( $privmsg['user_aim'] ) ? '<a href="aim:goim?screenname=' . $privmsg['user_aim'] . '&amp;message=Hello+Are+you+there?">' . $lang['AIM'] . '</a>' : '';

	$temp_url = append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id_from");
	$msn_img = ( $privmsg['user_msnm'] ) ? '<a href="' . $temp_url . '"><img src="' . $images['icon_msnm'] . '" alt="' . $lang['MSNM'] . '" title="' . $lang['MSNM'] . '" border="0" /></a>' : '';
	$msn = ( $privmsg['user_msnm'] ) ? '<a href="' . $temp_url . '">' . $lang['MSNM'] . '</a>' : '';

	$yim_img = ( $privmsg['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $privmsg['user_yim'] . '&amp;.src=pg"><img src="' . $images['icon_yim'] . '" alt="' . $lang['YIM'] . '" title="' . $lang['YIM'] . '" border="0" /></a>' : '';
	$yim = ( $privmsg['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $privmsg['user_yim'] . '&amp;.src=pg">' . $lang['YIM'] . '</a>' : '';

	$temp_url = append_sid("search.$phpEx?search_author=" . urlencode($username_from) . "&amp;showresults=posts");
	$search_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_search'] . '" alt="' . sprintf($lang['Search_user_posts'], $username_from) . '" title="' . sprintf($lang['Search_user_posts'], $username_from) . '" border="0" /></a>';
	$search = '<a href="' . $temp_url . '">' . sprintf($lang['Search_user_posts'], $username_from) . '</a>';

	//
	// Processing of post
	//
	$post_subject = $privmsg['privmsgs_subject'];

	$private_message = $privmsg['privmsgs_text'];
	$bbcode_uid = $privmsg['privmsgs_bbcode_uid'];

	if ( $board_config['allow_sig'] )
	{
		$user_sig = ( $privmsg['privmsgs_from_userid'] == $userdata['user_id'] ) ? $userdata['user_sig'] : $privmsg['user_sig'];
	}
	else
	{
		$user_sig = '';
	}

	$user_sig_bbcode_uid = ( $privmsg['privmsgs_from_userid'] == $userdata['user_id'] ) ? $userdata['user_sig_bbcode_uid'] : $privmsg['user_sig_bbcode_uid'];

	// get the sender display block
	if ( $privmsg['user_id_1'] == $user->data['user_id'] )
	{
		$view_user = $user;
	}
	else
	{
		$view_user = new user();
		$view_user->read($privmsg['user_id_1']);
	}

	// erase user sig if not attached
	if ( !$privmsg['privmsgs_attach_sig'] )
	{
		$view_user->data['user_sig'] = '';
		$view_user->data['user_sig_bbcode_uid'] = '';
	}

	// store the user
	$user->pool[ intval($view_user->data['user_id']) ] = $view_user->data;
	unset($view_user);

	// get poster info
	if ( !class_exists('topics') )
	{
		include($config->url('includes/class_topics'));
	}
	if ( !class_exists('posts') )
	{
		include($config->url('includes/class_posts'));
	}
	$poster_info = new posts($requester);
	$user_blocks = $poster_info->poster_block_vars($privmsg['user_id_1']);
	unset($poster_info);

	$tpl_switches = empty($user_blocks['switches']) ? array() : $user_blocks['switches'];
	$tpl_vars = empty($user_blocks['data']) ? array() : $user_blocks['data'];
	unset($user_blocks);

	// anon
	if ( $privmsg['user_id_1'] == ANONYMOUS )
	{
		$tpl_vars['POSTER_NAME'] = $user->lang('Guest');
	}

	// overwrite some vars: avatar, rank & sig
	$poster_avatar = !isset($tpl_vars['POSTER_AVATAR']) || empty($tpl_vars['POSTER_AVATAR']) ? '' : $template->sprintf('<img src="%s" alt="" border="0" />', $tpl_vars['POSTER_AVATAR']);
	$poster_rank = !isset($tpl_vars['L_POSTER_RANK']) || empty($tpl_vars['L_POSTER_RANK']) ? '' : $tpl_vars['L_POSTER_RANK'];
	$rank_image = !isset($tpl_vars['I_POSTER_RANK']) || empty($tpl_vars['I_POSTER_RANK']) ? '' : $template->sprintf('<img src="%s" alt="" title="%s" border="0" />', $tpl_vars['I_POSTER_RANK'], $poster_rank);

	// send to template
	if ( $tpl_vars )
	{
		$template->assign_vars($tpl_vars);
	}
	if ( $tpl_switches )
	{
		foreach ( $tpl_switches as $key => $value )
		{
			$template->set_switch($key, $value);
		}
	}

	// author panel constants
	$template->assign_vars(array(
		// legend
		'L_POSTER_POSTS' => $user->lang('Posts'),
		'L_POSTER_FROM' => $user->lang('Location'),
		'L_POSTER_JOINED' => $user->lang('Joined'),
		// users buttons
		'I_POSTER_PROFILE' => $user->img('icon_profile'),
		'L_POSTER_PROFILE' => $user->lang('Read_profile'),
		'I_POSTER_SEARCH' => $user->img('icon_search'),
		'I_POSTER_PM' => $user->img('icon_pm'),
		'L_POSTER_PM' => $user->lang('Send_private_message'),
		'I_POSTER_EMAIL' => $user->img('icon_email'),
		'L_POSTER_EMAIL' => $user->lang('Send_email'),
		'I_POSTER_WWW' => $user->img('icon_www'),
		'L_POSTER_WWW' => $user->lang('Visit_website'),
		'I_POSTER_ICQ' => $user->img('icon_icq'),
		'L_POSTER_ICQ' => $user->lang('ICQ'),
		'I_POSTER_AIM' => $user->img('icon_aim'),
		'L_POSTER_AIM' => $user->lang('AIM'),
		'I_POSTER_MSN' => $user->img('icon_msnm'),
		'L_POSTER_MSN' => $user->lang('MSNM'),
		'I_POSTER_YIM' => $user->img('icon_yim'),
		'L_POSTER_YIM' => $user->lang('YIM'),
		// back to top
		'I_BACK_TO_TOP' => $user->img('icon_go_top'),
		'L_BACK_TO_TOP' => $user->lang('Back_to_top'),
	));

	// other message components
	$post_subject = _censor($post_subject);

	// parse message & sig
	$message_parser = new message();
	$switches = array(
		'html' => $privmsg['privmsgs_enable_html'],
		'bbcode' => !empty($bbcode_uid),
		'smilies' => $privmsg['privmsgs_enable_smilies'],
	);
	$message_parser->parse($private_message, $bbcode_uid, $switches);
	unset($message_parser);

	//
	// Dump it to the templating engine
	//
	$template->assign_vars(array(
		'MESSAGE_TO' => $username_to,
		'MESSAGE_FROM' => $username_from,
		'RANK_IMAGE' => $rank_image,
		'POSTER_JOINED' => $poster_joined,
		'POSTER_FROM' => $poster_from,
		'POSTER_AVATAR' => $poster_avatar,
		'POST_SUBJECT' => $post_subject,
		'POST_DATE' => $post_date,
		'MESSAGE' => $private_message,

		'PROFILE_IMG' => $profile_img,
		'PROFILE' => $profile,
		'SEARCH_IMG' => $search_img,
		'SEARCH' => $search,
		'EMAIL_IMG' => $email_img,
		'EMAIL' => $email,
		'WWW_IMG' => $www_img,
		'WWW' => $www,
		'ICQ_STATUS_IMG' => $icq_status_img,
		'ICQ_IMG' => $icq_img,
		'ICQ' => $icq,
		'AIM_IMG' => $aim_img,
		'AIM' => $aim,
		'MSN_IMG' => $msn_img,
		'MSN' => $msn,
		'YIM_IMG' => $yim_img,
		'YIM' => $yim)
	);

	// plugs
		if ( $this->plug_ins )
		{
			foreach ( $this->plug_ins as $plug => $dummy )
			{
				if ( method_exists($this->plug_ins[$plug], 'display_message') )
				{
					$this->plug_ins[$plug]->display_message($privmsg, $privmsgs_id);
				}
			}
		}


//-----------------------------------

		global $db, $template, $config, $user;

		$template->set_switch('switch_standalone');
		$template->set_switch('switch_standalone.switch_user_logged_in', $user->data['session_logged_in']);
		$template->set_switch('switch_subject');

		// posting subject length
		if ( intval($config->data['topic_title_length']) <= 0 )
		{
			$config->set('topic_title_length', 60, true);
		}
		$template->assign_vars(array(
			'SUBJECT_LENGTH' => intval($config->data['topic_title_length']),

			'L_LEFT_PART' => $user->lang('Private_Message'),
			'I_SUBMIT' => $user->img('cmd_submit'),
			'I_PREVIEW' => $user->img('cmd_preview'),
			'I_CANCEL' => $user->img('cmd_cancel'),

			'L_GO' => $user->lang('Go'),
			'I_GO' => $user->img('cmd_mini_submit'),
			'PID' => _read('pid', TYPE_INT),
		));
		foreach ( $this->boxes as $box => $data )
		{
			$box_tpl = strtoupper($box);
			$tpl_vars += array(
				'U_' . $box_tpl => $config->url('privmsg', array('folder' => $box), true),
				'L_' . $box_tpl => $user->lang($data['txt']),
				'I_' . $box_tpl => $user->img($data['img']),
			);
			$template->set_switch($box, $this->folder == $box);
		}
		$template->assign_vars($tpl_vars);

		$template->set_switch('save_button', ($folder != 'outbox') || ($folder != 'savebox'));

		return;
	}

	// move a pm to another box (e.g. savebox, sent-box, etc)
	function move()
	{


		return;
	}

	// delete a pm
	function delete($id)
	{


		return;
	}
}

/*
A simple process for a pm should be:
$users = '3,6,12,89,163,...,821';
$subject = 'Hi Everyone!';
$message = 'This is a test message';
$subtitle = 'Testing';

$notify = new advnot_parent('pm');
$notify->add_users($users, 'to');
$notify->prepare($subject, $message, $subtitle);
$notify->send();
*/
?>